package com.xk.sport.backend.service;

import com.xk.sport.backend.dao.repository.AreaDao;
import com.xk.sport.backend.dao.repository.CommunityDao;
import com.xk.sport.backend.dao.model.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaService {
    @Autowired
    private AreaDao areaDao;
    @Autowired
    private CommunityDao communityDao;

    /**
     * 查询
     */
    public List<Area> findAll() {
        return areaDao.findAll();
    }

    /**
     * 查询
     */
    public Area findOne(long id) {
        return areaDao.findOne(id);
    }

    /**
     * 添加
     */
    public Area add(Area area) {
        if (area == null || area.getId() > 0) {
            return null;
        }
        area.setCreateTime(System.currentTimeMillis());
        return areaDao.save(area);
    }

    /**
     * 修改
     */
    public Area update(Area area) {
        if (area == null || area.getId() <= 0) {
            return null;
        }
        return areaDao.save(area);
    }

    /**
     * 删除
     */
    public boolean delete(long id) {
        areaDao.delete(id);
        communityDao.deleteCommunitiesByAreaId(id);
        return true;
    }
}
